package com.groupbuying.customer.service.impl;

import com.groupbuying.customer.connector.CurrencyFeign;
import com.groupbuying.customer.dao.CustomerUshopDao;
import com.groupbuying.customer.dto.CustomerCurrencyDTO;
import com.groupbuying.customer.dto.CustomerCurrencyLogDTO;
import com.groupbuying.customer.service.CustomerCurrencyService;
import com.groupbuying.customer.utils.DateFormatHelper;
import com.groupbuying.customer.utils.PageUtils;
import com.groupbuying.customer.utils.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * @Author: WANGNING
 * @Email wyn20140226@aliyun.com
 * @Date: 2019/8/28 14:07
 * @Des 用户积分
 */
@Service
public class CustomerCurrencyServiceImpl implements CustomerCurrencyService {
    private static final Logger logger = LoggerFactory.getLogger(CustomerCurrencyServiceImpl.class);
    @Autowired
    private CurrencyFeign currencyFeign;
    @Autowired
    CustomerUshopDao customerUshopDao;

    @Override
    public PageUtils customerCurrency(Map<String, Object> map) {
        PageUtils details = currencyFeign.getCurrencyList(map);
        return details;
    }

    @Override
    public PageUtils getCurrencyDetails(Map<String, Object> map) {
        Object startTime = map.get("start");
        Object endTime = map.get("end");
        Object tempMobile = map.get("mobile");
      /* String start = tempStartTime==null?"":tempStartTime.toString();
       String end = tempEndTime==null?"":tempEndTime.toString();
       String mobile = tempMobile==null?"":tempMobile.toString();
       String startTime = DateFormatHelper.format(new Date(Long.valueOf(start)), DateFormatHelper.FORMAT_DATE_STR)+" "+"00:00:00";
        String endTime = DateFormatHelper.format(new Date(Long.valueOf(end)), DateFormatHelper.FORMAT_DATE_STR)+" "+"23:59:59";*/

        String mobile = tempMobile==null?"":tempMobile.toString(); logger.info("开始时间:{}",startTime);
        logger.info("结束时间:{}",endTime);
        Map<String,Object> params = new HashMap<>();

        params.put("startTime",startTime);
        params.put("endTime",endTime);
        params.put("currencyType","01");
        params.put("currencyTypes","02");
        params.put("mobile",mobile);
        //查到时间，手机号，customer_id
        List<CustomerCurrencyDTO> currencyDetails = customerUshopDao.getDetail(params);
        logger.info("查询结果：{}",currencyDetails);
        Query query = new Query(params);
        int total = customerUshopDao.countForCurrency(query);


        logger.info("总数：{}",total);
        PageUtils pageUtils = new PageUtils(currencyDetails,total);
        return pageUtils;
    }

    @Override
    public Map<String,Object> getAllCurrency(Map<String, Object> map) {
        Object tempStartTime = map.get("start");
        Object tempEndTime = map.get("end");

        String start = tempStartTime==null?"":tempStartTime.toString();
        String end = tempEndTime==null?"":tempEndTime.toString();
        String startTime = DateFormatHelper.format(new Date(Long.valueOf(start)), DateFormatHelper.FORMAT_DATE_STR)+" "+"00:00:00";
        String endTime = DateFormatHelper.format(new Date(Long.valueOf(end)), DateFormatHelper.FORMAT_DATE_STR)+" "+"23:59:59";
        Map<String,Object> params = new HashMap<>();
        params.put("startTime",startTime);
        params.put("endTime",endTime);
        params.put("currencyType","01");
        params.put("currencyTypes","02");
        //期初
        BigDecimal startTimasCurrency = customerUshopDao.getStartTimasCurrency(params);
        //期末
        BigDecimal endTimasCurrency = customerUshopDao.getEndTimasCurrency(params);
        //增加
        BigDecimal addCurrencyNumber = customerUshopDao.getAddCurrencyNumber(params);
        //减少
        BigDecimal redCurrencyNumber = customerUshopDao.getRedCurrencyNumber(params);
        Map<String,Object> paramap = new HashMap<>();
        paramap.put("startTimasCurrency",startTimasCurrency);
        paramap.put("endTimasCurrency",endTimasCurrency);
        paramap.put("addCurrencyNumber",addCurrencyNumber);
        paramap.put("redCurrencyNumber",redCurrencyNumber);
        return paramap;
    }

    @Override
    public PageUtils getCurrencyLogByCustomerId(Map<String, Object> map) {
        Object tempStartTime = map.get("start");
        Object tempEndTime = map.get("end");
        String start = tempStartTime==null?"":tempStartTime.toString();
        String end = tempEndTime==null?"":tempEndTime.toString();
        String startTime = DateFormatHelper.format(new Date(Long.valueOf(start)), DateFormatHelper.FORMAT_DATE_STR)+" "+"00:00:00";
        String endTime = DateFormatHelper.format(new Date(Long.valueOf(end)), DateFormatHelper.FORMAT_DATE_STR)+" "+"23:59:59";
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        List<CustomerCurrencyLogDTO> currencyLogDTOList = customerUshopDao.getCurrencyLogByCustomerId(map);
        Query query = new Query(map);
        int total = customerUshopDao.countForCurrencyLog(query);
        PageUtils pageUtils=  new PageUtils(currencyLogDTOList,total);
        return pageUtils;



    }

    @Override

    public List<Map<String,Object>> getMouthCurrency(Map<String, String> map) {

        List<Map<String, Object>> mouthCurrency = customerUshopDao.getMouthCurrency(map);

        return mouthCurrency;
    }
}
